'use strict';
const db = uniCloud.database();
const _ = db.command;

exports.main = async (event, context) => {
    const { username, oldPassword, newPassword } = event;
    const numOldPassword = Number(oldPassword);
    const numNewPassword = Number(newPassword);
    const res = await db.collection('users').where({
        username: username,
        password: numOldPassword
    }).get();
    if (res.data.length > 0) {
        await db.collection('users').where({
            username: username
        }).update({
            password: numNewPassword
        });
        return {
            code: 200,
            msg: '密码修改成功'
        };
    } else {
        return {
            code: 401,
            msg: '旧密码错误'
        };
    }
};